package cn.mg.framework.util.common;

import org.apache.tomcat.util.codec.binary.Base64;
import java.io.*;

/**
 * @author ezhong
 */
public class Base64Util {
	
	//base64字符串转byte[]  
    public static byte[] base64String2ByteFun(String base64Str){  
        return Base64.decodeBase64(base64Str);
    }  
    //byte[]转base64  
    public static String byte2Base64StringFun(byte[] b){  
        return Base64.encodeBase64String(b);  
    }  
    
    /**
     * 
     * @param base64
     * @param fileName
     * @param filePath
     */
    public static String getFile(String base64, String fileName, String filePath) {  
    	byte[] bfile = base64String2ByteFun(base64);// 将字符串转换为byte数组
        BufferedOutputStream bos = null;  
        FileOutputStream fos = null;  
        File file = null;  
        try {  
            File dir = new File(filePath);  
            if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在  
                dir.mkdirs();  
            }  
            file = new File(filePath+"\\"+fileName);  
            fos = new FileOutputStream(file);  
            bos = new BufferedOutputStream(fos);  
            bos.write(bfile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (bos != null) {  
                try {  
                    bos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
            if (fos != null) {  
                try {  
                    fos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
        }
        return filePath+"\\"+fileName;
    }  
    
    
    
    /**
     * base64字符串转文件
     * @param base64
     * @return
     */
    public static File base64ToFile(String base64, String fileName, String filePath) {
        File file = null;
        FileOutputStream out = null;
        try {
            file = new File(filePath); 
            if (!file.exists()) {
                file.createNewFile();
            }
            byte[] bytes = base64String2ByteFun(base64);// 将字符串转换为byte数组
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            byte[] buffer = new byte[1024];
            out = new FileOutputStream(file);
            int bytesum = 0;
            int byteread = 0;
            while ((byteread = in.read(buffer)) != -1) {
                bytesum += byteread;
                out.write(buffer, 0, byteread); // 文件写操作
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (out!= null) {
                    out.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return file;
    }

    public static void main(String[] args){
    	String a = "fCuwR3bbGOUGdrsoj3vIwURZ10mO9iRl:8Iz3uwkOLyGjDmop";
    	String base64 = byte2Base64StringFun(a.getBytes());
    	System.out.println(base64);
    }
}
